package com.team.teamproject.ui.hot;

import android.content.Intent;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.team.teamproject.R;
import com.team.teamproject.adapter.HotRecommendAdapter;
import com.team.teamproject.adapter.firstViewPager;
import com.team.teamproject.bean.HotRecommend;
import com.team.teamproject.bean.Hotdetails;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class HotDetailesActivity extends AppCompatActivity {
        private String id;
    private Toolbar   myToolbar;
    private CollapsingToolbarLayout myCollapsingToolbarLayout;
    private ViewPager myViewPager;
    private TextView tv_hotName,tv_hotPrice,tv_hotFavourite,tv_Short;
    private ImageButton ib_Favourite;
    private GridView gv_Hot;
    private List<Hotdetails> lists;
    private String[] imageurlshuzhu;
    //private List<HotRecommend>  hotRecommendList;
    private List<String> recommendnamelist,recommendpricelist,image_urllist;
    private HotRecommendAdapter hotrecommendadpter;
    private  String recommendname,recommendprice,image_url;
    //private  List<String> image_urls;
    //private int length;
    private String name,price,short_description,favorites_count;
    //private firstViewPager    ViewPagerAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hot_detailes);
        recommendnamelist=new ArrayList<>();
        recommendpricelist=new ArrayList<>();
        image_urllist=new ArrayList<>();
        //获取id
        Intent intent=getIntent();
        id=intent.getStringExtra("id");
       //Log.i("hot","---->id"+id);
        //控件
        getid();
        //解析
        parson();
      //  ViewPagerAdapter=new firstViewPager(image_urls);
        //猜你喜欢
        parsoncommend();


    }


    //猜你喜欢
    private void parsoncommend() {
          new Retrofit.Builder()
                  .baseUrl("http://api.liwushuo.com/v2/")
                  .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                  .addConverterFactory(GsonConverterFactory.create())
                  .build()
                  .create(HotDetails.class)
                  .searchRecommend(id)
                  .subscribeOn(Schedulers.newThread())
                  .observeOn(AndroidSchedulers.mainThread())
                  .subscribe(new Action1<HotRecommend>() {
                      @Override
                      public void call(HotRecommend hotRecommend) {

                     for (int i=0;i<hotRecommend.getData().getRecommend_items().size();i++)
                     {
                         recommendname=hotRecommend.getData().getRecommend_items().get(i).getName();
                        // hotRecommendList.addAll(recommendname)
                         recommendprice=hotRecommend.getData().getRecommend_items().get(i).getPrice();
                         image_url=hotRecommend.getData().getRecommend_items().get(i).getCover_image_url();
                       //  Log.i("aaa","----------->"+"aaaaaaaaaaaa"+i);
                       //  Log.i("aaa","----------->"+"aaaaaaaaaaaa"+recommendname);
            ;
                         recommendnamelist.add(recommendname);
                        // Log.i("aaa","----------->"+"aaaaaaaaaaaa---->"+recommendnamelist.size());
                         recommendpricelist.add(recommendprice);
                         image_urllist.add(image_url);
                     }


                          hotrecommendadpter=new HotRecommendAdapter(HotDetailesActivity.this,recommendnamelist,recommendpricelist,image_urllist);
                          gv_Hot.setAdapter(hotrecommendadpter);
                          hotrecommendadpter.notifyDataSetChanged();

                      }
                  }, new Action1<Throwable>() {
                      @Override
                      public void call(Throwable throwable) {

                      }
                  });
    }

    private void parson() {
        new Retrofit.Builder()
                .baseUrl("http://api.liwushuo.com/")
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build()
                .create(HotDetails.class)
                .searchDetails(id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
               .subscribe(new Action1<Hotdetails>() {
                   @Override
                   public void call(Hotdetails hotdetails) {
                       name=hotdetails.getData().getName();
                       price=hotdetails.getData().getPrice();
                       short_description=hotdetails.getData().getDescription();
                       //Log.i("short","----->"+short_description);
                       favorites_count=hotdetails.getData().getFavorites_count()+"";
                       for (int i=0;i<hotdetails.getData().getImage_urls().size();i++)
                       {
                           imageurlshuzhu=new String[]{hotdetails.getData().getImage_urls().get(i)};
                          // Log.i("aaa","----->imageurlshuzhu"+imageurlshuzhu.toString());
                       }
                   // Log.i("aaa","----->imageurlshuzhu"+imageurlshuzhu.length);
                    //   image_urls= hotdetails.getData().getImage_urls();
                     //  length=image_urls.size();
                     // Log.i("image","----->"+image_urls.size());
                       tv_hotName.setText(name);
                       tv_hotPrice.setText(price);
                       tv_Short.setText(short_description);
                       tv_hotFavourite.setText(favorites_count);
                   }
               }, new Action1<Throwable>() {
                   @Override
                   public void call(Throwable throwable) {

                   }
               });



    }

    private void getid() {
        myToolbar= (Toolbar) findViewById(R.id.tool_detail);
        myCollapsingToolbarLayout= (CollapsingToolbarLayout) findViewById(R.id.collapse_layout);
        myViewPager= (ViewPager) findViewById(R.id.vp_detail);
        ib_Favourite= (ImageButton) findViewById(R.id.ib_Favourite);
        tv_hotName= (TextView) findViewById(R.id.tv_hotName);
        tv_hotPrice= (TextView) findViewById(R.id.tv_hotPrice);
        tv_hotFavourite= (TextView) findViewById(R.id.tv_hotFavourite);
        tv_Short= (TextView) findViewById(R.id.tv_Short);
        gv_Hot= (GridView) findViewById(R.id.gv_Hot);

    }
}
